/**
 * Created by win8 on 2016-10-11.
 */
var myScroll, pullDownEl, pullDownOffset, pullUpEl, pullUpOffset, generatedCount = 0, scrollDiv = '';

function loaded() {
    if (scrollDiv == '') {
        scrollDiv = 'wrapper-dsh';
    }

    pullDownEl = $("#" + scrollDiv + " .pullDown");
    pullDownOffset = pullDownEl.height();
    pullUpEl = $("#" + scrollDiv + " .pullUp");
    pullUpOffset = pullUpEl.height();
    if (myScroll)
        myScroll.destroy();
    myScroll = new iScroll(scrollDiv, {
        useTransition: true,
        topOffset: pullDownOffset,
        checkDOMChanges: true,
        onRefresh: function () {
            if (pullDownEl.className == "loading") {
                pullDownEl.className = "";
                pullDownEl.find(".pullDownLabel").text("下拉刷新...");
            } else if (pullUpEl.className == "loading") {
                pullUpEl.className = "";
                pullUpEl.find(".pullUpLabel").text("上拉显示更多...");
            }
        },
        onScrollMove: function () {
            if (this.y > 5 && pullDownEl.className != "flip") {
                pullDownEl.className = "flip";
                pullDownEl.find(".pullDownLabel").text("准备刷新...");
                this.minScrollY = 0;
            } else if (this.y < 5 && pullDownEl.className == "flip") {
                pullDownEl.className = "";
                pullDownEl.find(".pullDownLabel").text("下拉刷新...");
                this.minScrollY = -pullDownOffset;
            } else if (this.y < (this.maxScrollY - 5) && pullUpEl.className != "flip") {
                pullUpEl.className = "flip";
                pullUpEl.find(".pullUpLabel").text("准备刷新...");
                this.maxScrollY = this.maxScrollY;
            } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className == "flip") {
                pullUpEl.className = "";
                pullUpEl.find(".pullUpLabel").text("上拉显示更多...");
                this.maxScrollY = pullUpOffset;
            }
        },
        onScrollEnd: function () {
            if (pullDownEl.className == "flip") {
                pullDownEl.className = "loading";
                pullDownEl.find(".pullDownLabel").text("Loading...");
                //pullDownAction(param);
            } else if (pullUpEl.className == "flip") {
                pullUpEl.className = "loading";
                pullUpEl.find(".pullUpLabel").text("上拉显示更多...");
                pullUpEl.find('.pullUpLabel').text('Loading...');
                pullUpAction(); //当到底部时再上拉时加载数据// Execute custom function (ajax call?)
            }
        }
    });
    //loadAction();
}

// iscrool事件初始化
function isScrollDevice() {
    document.addEventListener('touchmove', function (e) {
        e.preventDefault();
    }, false);//阻止冒泡
    document.addEventListener('DOMContentLoaded', function () {
        setTimeout(loaded, 0);
    }, false);
}

window.onload = isScrollDevice();

//初始状态，加载数据
function loadAction() {
    var el, item;
    el = $("#" + scrollDiv + " .recordlist");
    for (var i = 0; i < 10; i++) {
        item = document.createElement('div');
        item += '<a href="shengoudanxq.html" class="sgls-item mt1">' +
            '<p>' +
            '<span class="title">申购单号：</span><span>申购-2016101300001</span>' +
            '</p>' +
            '<p>' +
            '<span class="title pull-left">申购部门：</span><span class="span3">酒水饮料</span>' +
            '<span class="pull-right"><i class="title">制单人：</i><i class="text-nowrap">张三</i></span>' +
            '</p>' +
            '<p>' +
            '<span class="title">申购时间：</span><span>2016.7.22 16:00</span>' +
            '</p>' +
            '<h4 class="block">' +
            '<span class="btn-chexiao" style="border-right:solid 1px #e6eaef"><img' +
            'src="../css/img/icon-chexiao.png" alt="" width="16" style="vertical-align: middle"/>&nbsp;撤消</span><span class="btn-shenhe"><img' +
            'src="../css/img/icon-shenhe.png" alt="" width="14" style="vertical-align: middle"/>&nbsp;审核</span>' +
            '</h4>' +
            '</a>';
        el.append(item);
    }
    myScroll.refresh();
}

//下拉刷新当前数据
function pullDownAction() {
    setTimeout(function () {
        //这里执行刷新操作
        myScroll.refresh();
    }, 400);
}

//上拉加载更多数据
function pullUpAction() {
    setTimeout(function () {
            var el, item;
            el = $("#" + scrollDiv + " .recordlist");
            for (var i = 0; i < 10; i++) {
                //根据当前显示的数据类型判断跳转
                if (scrollDiv == 'wrapper-dsh') {
                    item = '<a href="bianjishenggoudan.html" class="sgls-item mt1">';
                }
                else
                    item = '<a href="shengoudanxq.html" class="sgls-item mt1">';

                item += '<p>' +
                    '<span class="title">申购单号：</span><span>申购-2016101300001</span>' +
                    '</p>' +
                    '<p>' +
                    '<span class="title pull-left">申购部门：</span><span class="span3">酒水饮料</span>' +
                    '<span class="pull-right"><i class="title">制单人：</i><i class="text-nowrap">张三</i></span>' +
                    '</p>' +
                    '<p>' +
                    '<span class="title">申购时间：</span><span>2016.7.22 16:00</span>' +
                    '</p>';

                //根据当前显示的数据类型判断显示的内容
                switch (scrollDiv) {
                    case 'wrapper-dsh':
                        item += '<h4 class="block">' +
                            '<span class="btn-chexiao" style="border-right:solid 1px #e6eaef">' +
                            '<img src="..\\css\\img\\icon-chexiao.png" alt="" width="16" style="vertical-align: middle"/>&nbsp;撤消</span>' +
                            '<span class="btn-shenhe">' +
                            '<img src="..\\css/img\\icon-shenhe.png" alt="" width="14" style="vertical-align: middle"/>&nbsp;审核</span>' +
                            '</h4>';
                        break;
                    case 'wrapper-ysh':
                        item += '<h4 class="block">' +
                            '<span class="btn-fanshenhe">' +
                            '<img src="..\\css\\img\\icon-fanshenhe.png" alt="" width="15" style="vertical-align: middle"/>&nbsp;反审核</span>' +
                            '</h4>';
                        break;
                    case 'wrapper-yxd':
                        item += '<p>' +
                            '<span class="title pull-left">下单时间：</span><span class="span3 text-nowrap">2016.7.22 16:00</span>' +
                            '<span class="pull-right"><i class="title">下单人：</i><i class="text-nowrap">张三</i></span>' +
                            '</p>';
                        break;
                }
                item += '</a>';
                el.append(item);
            }
            myScroll.refresh();
        }
        ,
        400
    )
    ;
}
function changePull() {
    setTimeout(loaded, 100);
}
